package student

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/models"
	"project/pkg/validate"
	"strconv"
)

func (h SHandler) UpdateInfo(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateInfoReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:UpdateInfo with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:UpdateInfo with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	student := &models.StudentInfo{
		WxOpenID:       req.WxOpenID,
		Name:           req.Name,
		StudentID:      req.StudentID,
		Phone:          req.Phone,
		ClassId:        strconv.Itoa(req.ClassId),
		TeacherId:      strconv.Itoa(req.TeacherId),
		LeaveInfoCount: "0",
	}
	//检查学号是否重复
	exist, _, err := h.M.StudentCheckExist(student.StudentID)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentCheckExist err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		resp.SendCode(c, models.StudentCodeExist)
		return
	}
	//判断这个学生是否提交过
	exits, info, err := h.M.StudentCheckExistByOpenID(req.WxOpenID)
	if exits {
		//则这次是更新操作
		info.Name = req.Name
		info.StudentID = req.StudentID
		info.Phone = req.Phone
		info.ClassId = strconv.Itoa(req.ClassId)
		info.TeacherId = strconv.Itoa(req.TeacherId)
		err = h.M.StudentUpdateInfoBuID(info)
		if err != nil {
			zap.L().Error("Student:UpdateInfo h.M.StudentUpdateInfoBuID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.SendSuccess(c)
		return
	}
	//更新信息
	err = h.M.StudentUpdateInfoBuID(student)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentUpdateInfoBuID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}
func (h SHandler) UpdateInfoToTeacher(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateInfoToTeacherReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Student:UpdateInfo with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Student:UpdateInfo with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	student := &models.StudentInfo{
		WxOpenID:       req.WxOpenID,
		Name:           req.Name,
		StudentID:      req.StudentID,
		Phone:          req.Phone,
		ClassId:        req.ClassId,
		TeacherId:      req.TeacherId,
		LeaveInfoCount: "0",
	}
	//检查学号是否重复
	exist, _, err := h.M.StudentCheckExist(student.StudentID)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentCheckExist err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		resp.SendCode(c, models.StudentCodeExist)
		return
	}
	//判断这个学生是否提交过
	exits, info, err := h.M.StudentCheckExistByOpenID(req.WxOpenID)
	if exits {
		//则这次是更新操作
		info.Name = req.Name
		info.StudentID = req.StudentID
		info.Phone = req.Phone
		info.ClassId = req.ClassId
		info.TeacherId = req.TeacherId
		err = h.M.StudentUpdateInfoBuID(info)
		if err != nil {
			zap.L().Error("Student:UpdateInfo h.M.StudentUpdateInfoBuID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.SendSuccess(c)
		return
	}
	//更新信息
	err = h.M.StudentUpdateInfoBuID(student)
	if err != nil {
		zap.L().Error("Student:UpdateInfo h.M.StudentUpdateInfoBuID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}
